﻿#if WINDOWS_PHONE
#else
using System.Collections.Generic;
using Microsoft.Xna.Framework.Net;

namespace JGLib.Net
{
    public abstract class GameState : JGLib.States.State
    {
        protected NetworkSession NetSession { get; set; }
        protected PacketReader NetReader { get; set; }
        protected PacketWriter NetWriter { get; set; }

        public GameState(object ID, NetworkSession session, PacketReader netReader, PacketWriter netWriter)
            : base(ID)
        {
            this.NetSession = session;
            this.NetReader = netReader;
            this.NetWriter = netWriter;
        }

        protected NetworkGamer[] GetAllPlayers()
        {
            List<NetworkGamer> gamers = new List<NetworkGamer>();
            foreach (NetworkGamer gamer in this.NetSession.AllGamers)
                gamers.Add(gamer);
            return gamers.ToArray();
        }
        protected NetworkGamer[] GetRemotePlayers()
        {
            List<NetworkGamer> gamers = new List<NetworkGamer>();
            foreach (NetworkGamer gamer in this.NetSession.AllGamers)
                if (!gamer.IsLocal)
                    gamers.Add(gamer);
            return gamers.ToArray();
        }
        protected LocalNetworkGamer GetLocalPlayer()
        {
            foreach (NetworkGamer gamer in this.NetSession.AllGamers)
                if (gamer.IsLocal)
                    return (LocalNetworkGamer)gamer;
            return null;
        }

        protected void UpdateNetwork()
        {
            this.GetLocalPlayer().ProcessIncomingData(this.NetReader, ((reader) => this.ProcessIncomingData()));
        }
        protected abstract void ProcessIncomingData();

        protected void SendMessage(Message message, SendDataOptions options)
        {
            message.WriteMessage(this.NetWriter, this.GetLocalPlayer(), options);
        }
    }
}
#endif